home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1997 May / PC Plus Super CD Issue 127 (May 1997).iso / handson / handson.exe / EDIT2.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-02-03  |  3.0 KB  |  118 lines

  1. unit Edit2;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, Menus, StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     MainMenu1: TMainMenu;
  12.     Memo1: TMemo;
  13.     OpenDialog1: TOpenDialog;
  14.     SaveDialog1: TSaveDialog;
  15.     FileMnu: TMenuItem;
  16.     FileNew: TMenuItem;
  17.     FileSave: TMenuItem;
  18.     FileLoad: TMenuItem;
  19.     N1: TMenuItem;
  20.     FileExit: TMenuItem;
  21.     FileSaveAs: TMenuItem;
  22.     procedure FileNewClick(Sender: TObject);
  23.     procedure FileSaveClick(Sender: TObject);
  24.     procedure FileSaveAsClick(Sender: TObject);
  25.     procedure FileLoadClick(Sender: TObject);
  26.     procedure FileExitClick(Sender: TObject);
  27.   private
  28.     { Private declarations }
  29.   public
  30.     { Public declarations }
  31.   end;
  32.  
  33. var
  34.   Form1: TForm1;
  35.  
  36. implementation
  37.  
  38. {$R *.DFM}
  39.  
  40.  
  41. function ConfirmFileSave(FileName : string) : boolean;
  42. { put up a dialog box to confirm that the existing file should be
  43.   overwritten. This function returns True if the Yes button is pressed,
  44.   otherwise it returns false. Note that you can test for the
  45.   buttons used in dialogs with these constants: mrNone,mrOk,mrCancel,
  46.   mrAbort,mrRetry,mrIgnore,mrYes,mrNo,mrAbort,mrRetry,mrIgnore,mrAll }
  47.  
  48. begin
  49.     if MessageDlg(FileName + ' already exists. Save anyway?',
  50.                         mtConfirmation, mbYesNoCancel, 0)
  51.                         = mrYes then
  52.       ConfirmFileSave := true
  53.     else
  54.       ConfirmFileSave := false;
  55. end;
  56.  
  57. procedure TForm1.FileNewClick(Sender: TObject);
  58. begin
  59.   Memo1.Clear;
  60.   OpenDialog1.Filename := '*.*';
  61.   Caption := 'Text Editor - [Untitled]';
  62. end;
  63.  
  64. procedure TForm1.FileSaveClick(Sender: TObject);
  65. begin
  66.   if (OpenDialog1.Filename <> '')
  67.      and (OpenDialog1.Filename <> '*.*') then
  68.   begin
  69.     Memo1.Lines.SaveToFile(OpenDialog1.Filename);
  70.   end
  71.   else FileSaveAsClick(Sender);
  72. end;
  73.  
  74. procedure TForm1.FileSaveAsClick(Sender: TObject);
  75. var
  76.    SaveFile : boolean;
  77. begin
  78.    SaveFile := true;
  79.    with SaveDialog1 do
  80.     if Execute then
  81.     begin
  82.       if FileExists(FileName) then
  83.          SaveFile := ConfirmFileSave(FileName);
  84.       If SaveFile then
  85.       begin
  86.          Memo1.Lines.SaveToFile(Filename);
  87.          Caption := 'Text Editor - ' + ExtractFilename(FileName);
  88.          OpenDialog1.Filename := Filename;
  89.       end;
  90.     end;
  91. end;
  92.  
  93. procedure TForm1.FileLoadClick(Sender: TObject);
  94. begin
  95.   with OpenDialog1 do
  96.     if Execute then
  97.     begin
  98.       { if the specified file exists it loads it, otherwise
  99.         it displays an error message }
  100.       if FileExists(FileName) Then
  101.       begin
  102.           Memo1.Lines.LoadFromFile(FileName);
  103.           Caption := 'Text Editor - ' + ExtractFilename(FileName);
  104.       end
  105.       else
  106.         MessageDlg('Sorry. Can''t load this file. '+ FileName +
  107.                            ' does not exist!',
  108.                         mtInformation, [mbOK], 0);
  109.     end;
  110. end;
  111.  
  112. procedure TForm1.FileExitClick(Sender: TObject);
  113. begin
  114.   Close;
  115. end;
  116.  
  117. end.
  118.